<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Permissions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('permission');
	}
	public function ajax_permission_get(){
		$permissionEntity =  new CI_Permission_entity();
		$permission = $permissionEntity->init('id',$_REQUEST['id']);
		echo json_encode($permission);
	}
	public function ajax_permission_list(){
		$permissionEntity =  new CI_Permission_entity();
		$permissionList = $permissionEntity->getPermissionList();
		$data['permissionList'] = $permissionList;
		$this->load->view('admin/permissions/index', $data);
	}
	public function ajax_permission_save(){
		$result = array();
		$permissionEntity =  new CI_Permission_entity();
		if($_REQUEST['permission'] == null || $_REQUEST['permission'] == ""){
			$result['result'] = false;
			$result['message'] = lang('escort_permission_require');
		}else{
			if($this->_check_permission(0, $_REQUEST['permission'])){
				$result['result'] = false;
				$result['message'] = lang('escort_permission_name_existed');
			}else{
				$permissionEntity->setName($_REQUEST['permission']);
				$permissionEntity->setSlug(url_title(strtolower(convert_accented_characters($_REQUEST['permission']))));
				$id = $permissionEntity->save($permissionEntity);
				$result['result'] = true;
				$result['message'] =lang('escort_permission_save_success');
				$result['id'] =$id;
			}
		}
		echo json_encode($result);
	}
	public function ajax_permission_update(){
		$result = array();
		$permissionEntity =  new CI_Permission_entity();
		if($_REQUEST['permission'] == null || $_REQUEST['permission'] == ""){
			$result['result'] = false;
			$result['message'] = lang('escort_permission_require');
		}else{
			if($this->_check_permission($_REQUEST['id'], $_REQUEST['permission'])){
				$result['result'] = false;
				$result['message'] = lang('escort_permission_name_existed');
			}else{
				$permissionEntity->setName($_REQUEST['permission']);
				$permissionEntity->setSlug(url_title(strtolower(convert_accented_characters($_REQUEST['permission']))));
				$permissionEntity->setId($_REQUEST['id']);
				$permissionEntity->update();
				$result['result'] = true;
				$result['message'] = lang('escort_permission_save_success');
			}
		}
		echo json_encode($result);
	}
	public function ajax_permission_delete(){
		$result = array();
		$permissionEntity =  new CI_Permission_entity();
		$permissionEntity->delete($_REQUEST['id']);
		$result['message'] = lang('escort_permission_delete_success');
		echo json_encode($result);
	}
	function _check_permission($id, $name){
		$ci = &get_instance();
		return $ci->permission_entity->checkExistedName($id, $name);
	}
}
?>